package com.mall.admin.controller;

import com.mall.admin.service.PmsBrandService;
import com.mall.commons.utils.PageResult;
import com.mall.commons.utils.R;
import com.mall.domain.PmsBrand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brand")
public class PmsBrandController {

    @Autowired
    private PmsBrandService pmsBrandService;

    /**
     * 获取全部品牌列表
     *
     * @return
     */
    @GetMapping("/listAll")
    public R listAll() {
        List<PmsBrand> list = pmsBrandService.listAll();
        return R.success("", list);
    }

    /**
     * 根据品牌名称分页获取品牌列表
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @return
     */
    @GetMapping("/list")
    public R list(Integer pageNum,Integer pageSize,String keyword){
        PageResult pageResult=pmsBrandService.list(pageNum,pageSize,keyword);
        return R.success("",pageResult);
    }

    /**
     * 添加品牌
     *
     * @param pmsBrand
     * @return
     */
    @PostMapping("/create")
    public R create(@RequestBody PmsBrand pmsBrand) {
        Integer count = pmsBrandService.create(pmsBrand);
        if (count == null) {
            return R.error(401, "添加失败!");
        }
        return R.success("添加成功!", "");
    }

    /**
     * 删除品牌
     *
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    public R delete(@PathVariable Long id) {
        Integer count=pmsBrandService.delete(id);
        if (count==null){
            return R.error(401,"删除失败!");
        }
        return R.success("","删除成功!");
    }

    /**
     * 批量删除品牌
     * @param ids
     * @return
     */
    @PostMapping("/delete/batch")
    public R deleteBatch(@RequestParam List<Long>ids){
        Integer count=pmsBrandService.deleteBatch(ids);
        if (count==null){
            return R.error(401,"删除失败!");
        }
        return R.success("删除成功");
    }

    /**
     * 批量更新厂家制造商状态
     * @param ids
     * @param factoryStatus
     * @return
     */
    @PostMapping("/update/factoryStatus")
    public R factoryStatus(@RequestParam List<Long>ids,Integer factoryStatus){
        pmsBrandService.factoryStatus(ids,factoryStatus);
        return R.success("更新状态成功!");
    }

    /**
     * 批量更新显示状态
     * @param ids
     * @param showStatus
     * @return
     */
    @PostMapping("/update/showStatus")
    public R showStatus(@RequestParam List<Long>ids,Integer showStatus){
        pmsBrandService.showStatus(ids,showStatus);
        return R.success("更新状态成功");
    }

    /**
     * 品牌更新
     * @param id
     * @param pmsBrand
     * @return
     */
    @PostMapping("/update/{id}")
    public R updateBrand(@PathVariable Long id,@RequestBody PmsBrand pmsBrand){
        Integer count=pmsBrandService.updateBrand(id,pmsBrand);
        if (count == null) {
            return R.error(401,"更新品牌失败!");
        }
        return R.success("更新成功");
    }

    /**
     * 根据编号查询品牌信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R query(@PathVariable Long id){
       PmsBrand pmsBrand=pmsBrandService.query(id);
       return R.success(pmsBrand);
    }
}
